package hhm.wechat.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.thoughtworks.xstream.XStream;

import hhm.wechat.pojo.News;
import hhm.wechat.pojo.NewsMessage;

public class MessageUtil {
	public static final String MESSAGE_TEXT = "text";
	public static final String MESSAGE_NEWS = "news";
	public static final String MESSAGE_IMAGE = "image";
	public static final String MESSAGE_VOICE = "voice";
	public static final String MESSAGE_MUSIC = "music";
	public static final String MESSAGE_VIDEO = "video";
	public static final String MESSAGE_LINK = "link";
	public static final String MESSAGE_LOCATION = "location";
	public static final String MESSAGE_EVNET = "event";
	public static final String MESSAGE_SUBSCRIBE = "subscribe";
	public static final String MESSAGE_UNSUBSCRIBE = "unsubscribe";
	public static final String MESSAGE_CLICK = "CLICK";
	public static final String MESSAGE_VIEW = "VIEW";
	public static final String MESSAGE_SCANCODE = "scancode_push";

	public static Map<String, String> xmlToMap(HttpServletRequest request) throws IOException, DocumentException {

		Map<String, String> map = new HashMap<>();

		SAXReader reader = new SAXReader();

		InputStream ins = request.getInputStream();

		Document doc = reader.read(ins);

		Element root = doc.getRootElement();

		List<Element> list = root.elements();

		for (Element e : list) {

			map.put(e.getName(), e.getText());

		}
		ins.close();

		return map;

	}

	public static String objectToXml(Object obj, Map<String, Class> map) {

		XStream xStream = new XStream();

		for (Map.Entry<String, Class> m : map.entrySet()) {
			xStream.alias(m.getKey(), m.getValue());
		}

		return xStream.toXML(obj);

	}

	public static String objectToXml(Object obj, Class clazz) {

		XStream xStream = new XStream();
		xStream.alias("xml", clazz);
		return xStream.toXML(obj);

	}

	public static String createNewsMessage(String toUserName, String fromUserName) {
		String message = null;
		List<News> newsList = new ArrayList<News>();
		NewsMessage newsMessage = new NewsMessage();
		News news = new News();
		news.setTitle("库里");
		news.setDescription(
				"斯蒂芬·库里（Stephen Curry），1988年3月14日出生于美国俄亥俄州阿克伦（Akron, Ohio），美国职业篮球运动员，司职控球后卫，效力于NBA金州勇士队。斯蒂芬·库里于2009年通过选秀进入NBA后一直效力于勇士队，新秀赛季入选最佳新秀第一阵容；2014-15赛季随勇士队获得NBA总冠军；两次当选常规赛MVP，两次入选最佳阵容第一阵容，4次入选全明星赛西部首发阵容。斯蒂芬·库里于2010年随美国队获土耳其世锦赛冠军，2014年随美国队获西班牙篮球世界杯冠军。");
		news.setPicUrl("http://hhmwechat.ngrok.cc/images/127236784_14165333197181n.jpg");
		news.setUrl("www.baidu.com");
		newsList.add(news);
		
		News news2 = new News();
		news2.setTitle("库里");
		news2.setDescription(
				"斯蒂芬·库里（Stephen Curry），1988年3月14日出生于美国俄亥俄州阿克伦（Akron, Ohio），美国职业篮球运动员，司职控球后卫，效力于NBA金州勇士队。斯蒂芬·库里于2009年通过选秀进入NBA后一直效力于勇士队，新秀赛季入选最佳新秀第一阵容；2014-15赛季随勇士队获得NBA总冠军；两次当选常规赛MVP，两次入选最佳阵容第一阵容，4次入选全明星赛西部首发阵容。斯蒂芬·库里于2010年随美国队获土耳其世锦赛冠军，2014年随美国队获西班牙篮球世界杯冠军。");
		news2.setPicUrl("http://hhmwechat.ngrok.cc/images/127236784_14165333197181n.jpg");
		news2.setUrl("www.baidu.com");
		newsList.add(news2);

		newsList.add(news);
		newsMessage.setToUserName(fromUserName);
		newsMessage.setFromUserName(toUserName);
		newsMessage.setCreateTime(new Date().getTime());
		newsMessage.setMsgType(MESSAGE_NEWS);
		newsMessage.setArticles(newsList);
		newsMessage.setArticleCount(newsList.size());

		Map<String, Class> map = new HashMap<>();
		map.put("item", News.class);
		map.put("xml", NewsMessage.class);
		message = objectToXml(newsMessage, map);
		return message;
	}

}
